package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.TaskPointDao;
import cn.topevery.garden.entity.po.TaskPoint;
import cn.topevery.garden.entity.query.TaskPointQuery;
import cn.topevery.garden.entity.vo.TaskPointVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 检查任务-样点关联表
 */
@Repository
public class TaskPointDaoImpl extends BaseDaoImpl<TaskPoint> implements TaskPointDao {
    @Override
    public <P extends DbCsp> List<TaskPoint> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<TaskPointVO> listPage(TaskPointQuery query) {
        String table = "t_task_point ttp";

        String columns = "ttp.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ttp.c_id desc");
        sqlQueryMate
                .where(true, "ttp.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, TaskPointVO.class);
    }

    @Override
    public TaskPoint getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    /**
     * 根据任务ID或考评人员ID获取样点关联
     *
     * @param taskId           任务ID
     * @param evaluationUserId 考评
     * @return 任务样点关联
     */
    @Override
    public List<TaskPoint> listByTaskIdOrUserId(Integer taskId, Integer evaluationUserId) {
        String columns = "ttu.*";
        String table = "t_task_point ttu";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "ttu.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(taskId != null, "ttu.c_task_id", ScriptConditionEnum.Eq, taskId)
                .where(evaluationUserId != null, "ttu.c_evaluation_user_id", ScriptConditionEnum.Eq, evaluationUserId)
        ;
        return selectList(sqlQueryMate, TaskPoint.class);
    }
}
